
//Table A9 final run

log using "$rootpath/log/Table_A9_Results.log", replace
///This is the different quartiles and quintiles comp. 
eststo clear
use "$rootpath/processed_data/Estimation_Data.dta", clear
drop if key_estimation_sample==0
tab treatXpost if post==1
tab avg_old_code_prob if post==1
tab avg_old_code_prob
gen treat=1 if avg_old_code_prob>0.56
replace treat=0 if avg_old_code_prob<0.157
drop treatXpost
gen treatXpost=treat*post

reghdfe log_damage55 treatXpost if key_estimation_sample==1, absorb(facility_id n5#year) vce(cluster facility_id)
estimates store damage_d
summarize avg_old_code_prob if treatXpost>0&e(sample)==1
gen tempd1 = r(mean)
summarize avg_old_code_prob if treatXpost==0&post>0&e(sample)==1
gen tempd2 = r(mean)
gen tempd3=tempd1 - tempd2
summarize tempd3
display _b[treatXpost]/r(mean)
drop tempd1 tempd2 tempd3

///This is emissions
reghdfe  log_emissions4 treatXpost if key_estimation_sample==1, absorb(facility_id n5#year) vce(cluster facility_id)
estimates store emission_d
summarize avg_old_code_prob if treatXpost>0&e(sample)==1
gen tempd1 = r(mean)
summarize avg_old_code_prob if treatXpost==0&post>0&e(sample)==1
gen tempd2 = r(mean)
gen tempd3=tempd1 - tempd2
summarize tempd3
display _b[treatXpost]/r(mean)
drop tempd1 tempd2 tempd3

drop treat
gen treat=1 if avg_old_code_prob>0.475
replace treat=0 if avg_old_code_prob<0.24
drop treatXpost
gen treatXpost=treat*post

reghdfe  log_damage55 treatXpost if key_estimation_sample==1, absorb(facility_id n5#year) vce(cluster facility_id)
estimates store damage_q
///This is emissions. 
summarize avg_old_code_prob if treatXpost>0&e(sample)==1
gen tempd1 = r(mean)
summarize avg_old_code_prob if treatXpost==0&post>0&e(sample)==1
gen tempd2 = r(mean)
gen tempd3=tempd1 - tempd2
summarize tempd3
display _b[treatXpost]/r(mean)
drop tempd1 tempd2 tempd3

///This is emissions. 
reghdfe  log_emissions4 treatXpost if key_estimation_sample==1, absorb(facility_id n5#year) vce(cluster facility_id)
estimates store emission_q
summarize avg_old_code_prob if treatXpost>0&e(sample)==1
gen tempd1 = r(mean)
summarize avg_old_code_prob if treatXpost==0&post>0&e(sample)==1
gen tempd2 = r(mean)
gen tempd3=tempd1 - tempd2
summarize tempd3
display _b[treatXpost]/r(mean)
drop tempd1 tempd2 tempd3

esttab damage_d damage_q emission_d emission_q using "$rootpath/table_graph/TBL_A9.tex",  star( * 0.1 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) keep(treatXpost _cons) scalars(N F) ar2 replace 
 
log close
